package net.ahlawat.file;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

/**
 * The panel that encapsulates splitting option
 * @author Pranay Ahlawat
 */
public class SplitterPanel extends JPanel {
    private JTextField inputFileName = new JTextField("File to split");
    private JFileChooser fileChooser = new JFileChooser();
    private JTextField outputDirName = new JTextField("Output directory");
    private JButton chooseInputFile = new JButton("...");
    private JButton chooseOutputDirectory = new JButton("...");

    public SplitterPanel() {
        setLayout(new GridLayout(0,3));
        add(new JLabel("File to split"));
        add(inputFileName);
        add(chooseInputFile);
        add(new JLabel("Choose output directory"));
        add(outputDirName);
        add(chooseOutputDirectory);

        //add listeners
        chooseInputFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int choice = fileChooser.showOpenDialog(SplitterPanel.this);
                if (choice != JFileChooser.CANCEL_OPTION) {
                    File f = fileChooser.getSelectedFile();
                    inputFileName.setText(f.getAbsolutePath());
                    outputDirName.setText(f.getParent());
                }
            }
        });

        chooseOutputDirectory.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int choice = fileChooser.showOpenDialog(SplitterPanel.this);
                if (choice != JFileChooser.CANCEL_OPTION) {
                    File f = fileChooser.getSelectedFile();
                    outputDirName.setText(f.getAbsolutePath());
                }
            }
        });
    }
}
